<?php

  echo "<h2>Search Results</h2>";

  $connection = mysql_connect( "localhost", "root", "abc123" );
  
  if ( !$connection )
  {
    die( 'Could not connect: ' . mysql_error() );
  }  
  
  mysql_select_db( "sasystemTables", $connection );

  $query = "SELECT * FROM Logins WHERE role = 'student'";

  $query2 = "SELECT userID, name FROM Logins WHERE role = 'teacher'";

  $result = mysql_query( $query, $connection );

  $rowResults = mysql_num_rows( $result );

  if ( $rowResults == 0 )
  {
    mysql_free_result( $result );
  }
  else
  {
    echo "<form name = 'anotherForm' action = 'runResults.php' method = 'GET'>";
    echo "<table border = '1' >";

    $i = 0;
    
    while( $row = mysql_fetch_array( $result ) )
    {
      $var = $row['userID'];
      
      $result2 = mysql_query( $query2, $connection );
      
      $stringToOutput = "<tr><td><select name = 'student" . $i . "'>
                        <option value='$var'>" . $row['name'] . "</option></select></td>
                        <td><select name = 'teacherPairings" . $i . "'>";

      $stringToOutput .= "<option value='notAssigned'>No mentor</option>";

       while ( $row2 = mysql_fetch_array( $result2 ) )
       {
         $var2 = $row2['userID'];
         $var3 = $row2['name'];
         $stringToOutput .= "<option value = '$var2'>" . $var3 . "</option>";
       }

       $stringToOutput .= "</select></td></tr>";

       echo $stringToOutput;

      $i++;
    }

    echo "</table>";
    echo "<br>";
    echo "<table>";

    echo "<tr><td><input type='hidden' name='i' value = '$i'></td></tr>";

    echo "<tr><td><input type = 'button' value = 'Back To Matching'/></td>&nbsp;&nbsp;&nbsp;&nbsp;
          <td><input type = 'submit' value = 'Pair selected students and mentors'/></td></tr>";
    echo "</table>";
    echo "</form>";

    echo "<form name = 'done' action = 'emailFromAssignments.php' method = 'GET'>";
    echo "<table>";
    echo "<tr><td><input type = 'submit' value = 'Match students and send out confirmation e-mails'></td></tr>";
    echo "</table>";
    echo "</form>";
  }
 
  mysql_close( $connection );

?>
